﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Newtonsoft.Json.Linq;
using NLog;
using System;

namespace GBT.Tool.Core
{
    public class SmsManage
    {
        private Logger logger = LogManager.GetCurrentClassLogger();

        public bool SendManage(string mobile, string code)
        {
            IClientProfile profile = DefaultProfile.GetProfile("default", "LTAI4FwaRUez5DjqpVVzccuE", "Kz6VeXPG9cffiZbit303XSzn1yK8yo");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", mobile);
            request.AddQueryParameters("SignName", "启思报餐");
            request.AddQueryParameters("TemplateCode", "SMS_173690710");
            request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");

            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                string responseJson = System.Text.Encoding.Default.GetString(response.HttpResponse.Content);
                JObject result = JObject.Parse(responseJson);
                string resultCode = (string)result["Code"];

                logger.Info(result);
                Console.WriteLine(result);
                Console.WriteLine(resultCode);

                if (resultCode == "isv.MOBILE_NUMBER_ILLEGAL")
                {
                    return false;
                }

            }
            catch (ServerException e)
            {
                logger.Error(e);
                throw;
            }
            catch (ClientException e)
            {
                logger.Error(e);
                throw;
            }

            return true;
        }
    }
}
